//package com.example.springboot1.utils;
//
///**
// * @author ckj
// * @description TODO
// * @date 2023-05-24 17:35
// */
//
//
//import com.deepoove.poi.XWPFTemplate;
//import com.deepoove.poi.data.Pictures;
//import com.documents4j.api.DocumentType;
//import com.documents4j.api.IConverter;
//import com.documents4j.job.LocalConverter;
//import org.jodconverter.DocumentConverter;
//import org.jodconverter.document.DefaultDocumentFormatRegistry;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.mock.web.MockMultipartFile;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.*;
//import java.net.URLEncoder;
//import java.nio.charset.StandardCharsets;
//import java.util.HashMap;
//import java.util.Map;
//
//@RestController
//public class Word2Pdf {
//    @Autowired
//    private DocumentConverter documentConverter;
//     @GetMapping("/pdf")
//    public void word2pdf() throws IOException {
//
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("templates/template.docx");
//        Map<String, Object> data = new HashMap<>();
//
////            data.put("text","<h1>This is a heading</h1><p>This is a paragraph.</p><ul><li>Item 1</li><li>Item 2</li></ul>");
//        data.put("text", "<h1>text22</h1>");
//        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream).render(data);
//
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
////        FileOutputStream out = new FileOutputStream("output.docx");
//        template.write(out);
//        // 参考：https:
//        //blog.csdn.net/ka3p06/article/details/125476270 通过documents4j实现
//        InputStream docxInputStream = null;
//        ByteArrayOutputStream outputStream= new ByteArrayOutputStream();;
//        try {
//            // 原word地址
//            docxInputStream = new ByteArrayInputStream(out.toByteArray());
//            // 转换后pdf生成地址
//             IConverter converter = LocalConverter.builder()
//                     .baseFolder(new File(System.getProperty("java.io.tmpdir")))
//                     .build();
//            converter.convert(docxInputStream)
//                    .as(DocumentType.DOCX)
//                    .to(outputStream)
//                    .as(DocumentType.PDF).execute();
//            // 关闭
//            converter.shutDown();
//            // 关闭
//            outputStream.close();
//            // 关闭
//            docxInputStream.close();
//        } catch (Exception e) {
//            System.out.println("[documents4J] word转pdf失败:" + e.toString());
//        } finally {
//            if (outputStream != null) {
//                outputStream.close();
//            }
//            if (docxInputStream != null) {
//                docxInputStream.close();
//            }
//        }
//        String fileName = "确认表.pdf";
//        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//        try {
//            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//            String userAgent = request.getHeader("User-Agent");
//            if (userAgent.toLowerCase().indexOf("trident") > -1
//                    || userAgent.toLowerCase().indexOf("edge") > -1) {
//                fileName = URLEncoder.encode(fileName, "UTF-8");
//            } else {
//                fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
//            }
//            response.setContentType("application/pdf");
//            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//            response.setCharacterEncoding("utf-8");
//            OutputStream out1 = response.getOutputStream();
//            out1.write(outputStream.toByteArray());
//            out1.flush();
//            out1.close();
//        } catch (Exception e) {
//         }
//
//    }
//
//
//
//
//
//    @GetMapping("/pdf2")
//    public void word2pdf2() throws Exception {
//        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//        ClassLoader classLoader = this.getClass().getClassLoader();
//
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("templates/template.docx");
//        Map<String, Object> data = new HashMap<>();
//
////            data.put("text","<h1>This is a heading</h1><p>This is a paragraph.</p><ul><li>Item 1</li><li>Item 2</li></ul>");
//        data.put("text", "<h1>text22</h1>");
//        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream).render(data);
//
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
////        FileOutputStream out = new FileOutputStream("output.docx");
//        template.write(out);
//        // 参考：https:
//        //blog.csdn.net/ka3p06/article/details/125476270 通过documents4j实现
//        InputStream docxInputStream = null;
//        ByteArrayOutputStream outputStream= new ByteArrayOutputStream();;
//        try {
//            // 原word地址
//            docxInputStream = new ByteArrayInputStream(out.toByteArray());
//            // 转换后pdf生成地址
//            documentConverter
//                    .convert(docxInputStream)
//                    .as(DefaultDocumentFormatRegistry.DOCX)
//                    .to(response.getOutputStream())
//                    .as(DefaultDocumentFormatRegistry.PDF)
//                    .execute();
//            // 关闭
//            outputStream.close();
//            // 关闭
//            docxInputStream.close();
//        } catch (Exception e) {
//            System.out.println("[documents4J] word转pdf失败:" + e.toString());
//        } finally {
//            if (outputStream != null) {
//                outputStream.close();
//            }
//            if (docxInputStream != null) {
//                docxInputStream.close();
//            }
//        }
//        String fileName = "确认表.pdf";
//        try {
//            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//            String userAgent = request.getHeader("User-Agent");
//            if (userAgent.toLowerCase().indexOf("trident") > -1
//                    || userAgent.toLowerCase().indexOf("edge") > -1) {
//                fileName = URLEncoder.encode(fileName, "UTF-8");
//            } else {
//                fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
//            }
//            response.setContentType("application/pdf");
//            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//            response.setCharacterEncoding("utf-8");
//            OutputStream out1 = response.getOutputStream();
//            out1.write(outputStream.toByteArray());
//            out1.flush();
//            out1.close();
//        } catch (Exception e) {
//        }
//
//    }
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//    public static void main(String[] args) throws Exception {
//        Word2Pdf bean = new Word2Pdf();
//        bean.word2pdf();
//    }
//
//
//    public void upload() throws IOException{
//
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("template.docx");
//        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream);
//        String picPath = "http://140.246.86.11:9093/group1/M00/00/0C/rB0AKmNxBJaAVo-KAA1LWx2aTUs74.jpeg";
//        template.render(
//                new HashMap<String, Object>() {
//                    {
//                        put("picture", Pictures.ofUrl(picPath).size(100, 100).create());
//                        put("title","wwww");
//                    }
//                });
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
////        FileOutputStream out = new FileOutputStream("output.docx");
//        template.write(out);
//        // 参考：https:
//        //blog.csdn.net/ka3p06/article/details/125476270 通过documents4j实现
//        InputStream docxInputStream = null;
//        ByteArrayOutputStream outputStream = null;
//        try {
//            // 原word地址
//            docxInputStream = new ByteArrayInputStream(out.toByteArray());
//            // 转换后pdf生成地址
//            outputStream = new ByteArrayOutputStream();
//            IConverter converter = LocalConverter.builder().build();
//            converter.convert(docxInputStream)
//                    .as(DocumentType.DOCX)
//                    .to(outputStream)
//                    .as(DocumentType.PDF).execute();
//
//            MultipartFile file = new MockMultipartFile("新文件名.pdf",outputStream.toByteArray());
//
//
//            // 关闭
//            converter.shutDown();
//            // 关闭
//            outputStream.close();
//            // 关闭
//            docxInputStream.close();
//        } catch (Exception e) {
//            System.out.println("[documents4J] word转pdf失败:" + e.toString());
//        } finally {
//            if (outputStream != null) {
//                outputStream.close();
//            }
//            if (docxInputStream != null) {
//                docxInputStream.close();
//            }
//        }
//
//
//
//    }
//}
